﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LibraryManagement.Custom_Controls.Data_Grid_View;

namespace LibraryManagement.Inventory
{
    public partial class InvBookSubmit : Form
    {
        private decimal? id;
        private frmSelectMembers frmSelectMembers;
        private LibManagementEntities context;

        public InvBookSubmit()
        {
            InitializeComponent();
        }

        public InvBookSubmit(decimal? id, frmSelectMembers frmSelectMembers)
        {
            this.id = id;
            this.frmSelectMembers = frmSelectMembers;
            InitializeComponent();
        }

        private void libTableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }



        private void InvBookSubmit_Load(object sender, EventArgs e)
        {
            SetDataGrid();
            MemberDetail ma = context.MemberDetails.Single(x => x.Id == id);
            libTextBox1.Text = ma.Name;
            libTextBox2.Text = ma.MemberEnrollmentNo;
            libTextBox3.Text = ma.Branch1.BranchName;
            libTextBox4.Text = ma.Year1.YearInWords;
            libTextBox5.Text = context.BookTransactions.Where(x => x.MemberId == id && x.Issued == true).Count().ToString();
            libTextBox6.Text = context.BookTransactions.Where(x => x.MemberId == id).Sum(x => x.Dues).ToString();

        }

        private void SetDataGrid()
        {
            dgvMembers.Columns.Add(new libDataGridViewColumnSubmit());
            context = new LibManagementEntities();
            List<BookTransaction> listbooksubmit = context.BookTransactions.Where(x => x.MemberId == id && x.Issued == true).ToList();
            dgvMembers.DataSource = listbooksubmit;
        }

        private void InvBookSubmit_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmSelectMembers.Enabled = true;
        }

        private void dgvMembers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            LibManagementEntities context = new LibManagementEntities();
            var id =(decimal)dgvMembers.Rows[e.RowIndex].Cells["Id"].Value;
            BookTransaction book = context.BookTransactions.Single(x => x.Id == id);
            book.Issued = false;
            context.SaveChanges();

            InvBookSubmit_Load(sender, e);
        }
    }
}
